#!/bin/bash

# Inputs
INPUT_FILE=$1

# Outputs
OUTPUTS_DIRECTORY=outputs
TIME_RESULTS_FILE=$OUTPUTS_DIRECTORY"/"$INPUT_FILE".results"

# Executables
CUDAPSAT=../Debug/GPUSATSolver

if [[ "$#" != 1 ]] && [[ "$#" != 2 ]] && [[ "$#" != 3 ]] 
then
	echo "Wrong number of parameters. Expected 1 or 2." >&2
	exit 1
fi

if ! [[ -e "$CUDAPSAT" ]]
then
	echo "Could not find the solver." >&2
	exit 1
fi

if [[ "$#" == 1 ]]
then
	BLOCKS=1
else
	BLOCKS=$2

	if [[ "$BLOCKS" == "0" ]]
	then
		echo "Invalid number of blocks, should be a positive integer (if you recall your math classes, zero is not a positive integer)." >&2
		exit 1

	fi

	if ! [[ "$BLOCKS" =~ ^[1-9][0-9]*$ ]]
	then
		echo "Invalid number blocks, should be a positive integer." >&2
		exit 1
	fi
fi

if [[ "$#" == 1 ]] || [[ "$#" == 2 ]]
then
	THREADS=1
else
	THREADS=$3

	if [[ "$THREADS" == "0" ]]
	then
		echo "Invalid number of threads, should be a positive integer (if you recall your math classes, zero is not a positive integer)." >&2
		exit 1

	fi

	if ! [[ "$THREADS" =~ ^[1-9][0-9]*$ ]]
	then
		echo "Invalid number threads, should be a positive integer." >&2
		exit 1
	fi
fi


echo "File = $INPUT_FILE" >> $TIME_RESULTS_FILE
echo "Blocks = $BLOCKS" >> $TIME_RESULTS_FILE
echo "Threads = $THREADS" >> $TIME_RESULTS_FILE
#/usr/bin/time -o $TIME_RESULTS_FILE -a $CUDAPSAT $INPUT_FILE -b $BLOCKS -v 1 -p -s u >> $TIME_RESULTS_FILE
/usr/bin/time -o $TIME_RESULTS_FILE -a $CUDAPSAT $INPUT_FILE -b $BLOCKS -t $THREADS -v 2 -u OFF -s u >> $TIME_RESULTS_FILE
echo >> $TIME_RESULTS_FILE
